<?php
	session_start();
	require_once("../includes/public.inc.php");
	$username = isset($_SESSION['username'])?$_SESSION['username']:'';
	$type = isset($_GET['type'])?$_GET['type']:'';
	$act = isset($_GET['act'])?$_GET['act']:'';
	$s = isset($_POST['s'])?trim($_POST['s']):'';
//	echo strlen($s);exit;
	if ($act=='yz')
	{
		$user = mysql_fetch_array(mysql_query("select * from ".DBQIAN."users where username='".$username."' order by id desc limit 1"));
		if ($s)
		{
			if (!is_numeric($s)) 
			{ 
			 	echo "<script>alert('防伪码只能是数字');history.back();</script>";	
			}
			if(strlen($s)>10&&strlen($s)<17)
			{
				if(!empty($username))
				{
					$exist_fwm = mysql_fetch_array(mysql_query("select * from ".DBQIAN."fwm where fwm='".$s."' and hystatus=0 order by id desc limit 1"));
					if ($exist_fwm)
					{
						//修改fwm表hystatus为1 修改users表积分，增加积分记录表的积分记录；算出积分和也可以也可以在之前的基础上添加这些
						//echo "UPDATE ".DBQIAN."fwm SET hystatus=1,hytime='".date('Y-m-d H:i:s', time())."' where fwm ='".$s."'";exit;
						$hystatus =  mysql_query("UPDATE ".DBQIAN."fwm SET hystatus=1,hytime='".date('Y-m-d H:i:s', time())."' where fwm ='".$s."'");
						$usercredit = mysql_query("UPDATE ".DBQIAN."users SET credit=credit+".($exist_fwm['credit']*10)." where username='".$username."'");
						$creditrecord = mysql_query("insert into ".DBQIAN."creditrecord(username,credit,prex,time,fwm,product_id) values('".$username."',".($exist_fwm['credit']*10).",'','".date('Y-m-d H:i:s',time())."','".$s."',0)");
						if ($creditrecord&&$usercredit&&$hystatus)
						{
							echo "<script>alert('积分领取成功！');history.back();</script>";	
							exit;
						}
						else
						{
							//print_r($exist_fwm);exit;
							$users = mysql_fetch_array(mysql_query("select * from ".DBQIAN."users where username='".$username."' order by id desc limit 1"));
							mysql_query("delete from ".DBQIAN."creditrecord where fwm='".$s."'");
							mysql_query("update ".DBQIAN."fwm set hystatus=0,hytime='".date('Y-m-d H:i:s', time())."' where fwm ='".$s."'");
							if ($users['credit']>$user['credit'])
							{
								mysql_query("update ".DBQIAN."users set credit=credit-".($exist_fwm['credit']-10)." where username=.'".$username."'");
							}
							echo "<script>alert('积分领取失败！');history.back();</script>";	
							exit;
						}
					}
					else 
					{
						$exist_fwm1 = mysql_fetch_array(mysql_query("select * from ".DBQIAN."fwm where fwm='".$s."' and hystatus=1 order by id desc limit 1"));
						if ($exist_fwm1)
						{
							echo "<script>alert('积分已经被领取！');history.back();</script>";	
							exit;
						}
						else 
						{
							echo "<script>alert('没有该防伪码，请谨慎使用该产品！');history.back();</script>";	
							exit;
						}
					}
				}
				else 
				{
					echo "<script>alert('亲你还没有登录领取哦哦');history.back();</script>";	exit;
				}
			}
			else 
			{
				echo "<script>alert('防伪码要11到16位之间');history.back();</script>";exit;
			}
		}
		else 
		{
			echo "<script>alert('防伪码不能为空');history.back();</script>";exit;
		}
	}
?>
<!DOCTYPE html PUBLIC "-//WAPFORUM//DTD XHTML Mobile 1.0//EN" "http://www.wapforum.org/DTD/xhtml-mobile10.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="user-scalable=yes, width=device-width, initial-scale=1.0, maximum-scale=1.0">
<title>赚取积分</title>
<link rel="stylesheet" type="text/css" href="css/zhuanqu.css" media="all">
</head>
<body>
	<div class="pz-css">
		<ul class="top-tool">
			<li class="title">赚取积分</li>
			<li class="back"><a href="javascript:history.go(-1)"></a></li>
			<li class="home"><a href="index.php?type=<?=$type?>"></a></li>
		</ul>
	<form action="zhuanqu.php?act=yz&type=<?=$type?>" method="post" name="FW" >
			<div class="pz-panel">
				<h4>赚取积分：</h4>
				<h5>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;请刮开产品外包装盒上防伪标签的银色涂层获取积分码，将积分码输入后点击获取即可获得积分。</h5>
				<div class="pz-panel">
					<h5>温馨提示：积分码只能首次积分，重复将不再累计积分。</h5>
				</div>
			</div>
			<div class="pz-panel">
				<span class="span4"><label>积分码：</label></span>
				<span class="span8"><input value="" id="s" name="s" class="input" type="text"></span>
				<div class="span12"><button class="red">点击获取</button></div>
			</div>
		</form>
	</div>
</body>
</html>